<template>
  <div class="app-container">
    <div class="search-content">
      <div class="title">管理员查询</div>
      <div class="search-area">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="用户名">
            <el-input v-model="formInline.username" placeholder="用户名" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitByUser">查询</el-button>
          </el-form-item>
          <el-form-item label="ID" class="by-user">
            <el-input v-model="formInline.id" placeholder="id" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitById">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色" align="center">
        <template slot-scope="scope">
          {{ scope.row.role.role_name }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.admin_status | statusFilter">{{ scope.row.admin_status === 0 ? '正常' : '冻结' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="avatar" label="头像" width="300">
        <template slot-scope="scope">
          <span>{{ scope.row.avatar }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="240">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ format(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: null,
      currentPage: 1,
      formInline: {
        id: '',
        username: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleEdit(row) {
      this.$router.push(`/admin/addAdmin/${row.id}`)
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`/cms/admin/delete/${row.username}`).then(res => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async onSubmitByUser() {
      const res = await this.$http.get(`/cms/admin/searchByUsername?username=${this.formInline.username}`)
      this.list = res.data.rows
      this.total = res.data.count
    },
    async onSubmitById() {
      const res = await this.$http.get(`/cms/admin/searchById?id=${this.formInline.id}`)
      this.list = res.data.rows
      this.total = res.data.count
    },
    format(date) {
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    async fetchData() {
      const res = await this.$http.get(`/cms/admin/list?offset=${this.currentPage}`)
      this.list = res.data.rows
      this.total = res.data.count
      this.listLoading = false
    }
  }
}
</script>
<style lang="scss" scoped>
  .app-container{
    .search-content{
      .search-area{
        margin-top: 20px;
      }
    }
    .by-user{
      margin-left: 30px;
    }
  }
</style>

